﻿using System;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.InteropServices;
using Teigha.Core;

namespace Teigha.TD
{
	public class OdCrypt : OdRxObject
	{
		[EditorBrowsable(EditorBrowsableState.Never)]
		public OdCrypt(IntPtr cPtr, bool cMemoryOwn) : base(TD_DbPINVOKE.OdCrypt_SWIGUpcast(cPtr), cMemoryOwn)
		{
			this.swigCPtr = new HandleRef(this, cPtr);
		}

		[EditorBrowsable(EditorBrowsableState.Never)]
		public static HandleRef getCPtr(OdCrypt obj)
		{
			if (obj != null)
			{
				return obj.swigCPtr;
			}
			return new HandleRef(null, IntPtr.Zero);
		}

		~OdCrypt()
		{
			this.Dispose();
		}

		public override void Dispose()
		{
			if (this.swigCPtr.Handle != IntPtr.Zero)
			{
				if (this.swigCMemOwn)
				{
					lock (this.locker)
					{
						if (this.swigCPtr.Handle != IntPtr.Zero && this.swigCMemOwn)
						{
							this.swigCMemOwn = false;
							TD_DbPINVOKE.delete_OdCrypt(this.swigCPtr);
						}
					}
					this.swigCMemOwn = false;
				}
				this.swigCPtr = new HandleRef(null, IntPtr.Zero);
			}
			GC.SuppressFinalize(this);
			base.Dispose();
		}

		public OdCrypt() : this(TD_DbPINVOKE.new_OdCrypt(), MemoryManager.GetMemoryManager().GetCurrentTransaction() == null)
		{
			MemoryTransaction currentTransaction = MemoryManager.GetMemoryManager().GetCurrentTransaction();
			if (currentTransaction != null)
			{
				currentTransaction.AddObject(new OdCrypt(this.swigCPtr.Handle, true));
			}
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
			this.SwigDirectorConnect();
		}

		public new static OdCrypt cast(OdRxObject pObj)
		{
			MemoryManager memoryManager = MemoryManager.GetMemoryManager();
			MemoryTransaction currentTransaction = memoryManager.GetCurrentTransaction();
			IntPtr p = TD_DbPINVOKE.OdCrypt_cast(OdRxObject.getCPtr(pObj));
			OdCrypt result;
			if (currentTransaction != null)
			{
				currentTransaction.AddObject((OdCrypt)Teigha.Core.Helpers.odrxCreateObjectInternal(p, true));
				result = (OdCrypt)Teigha.Core.Helpers.odrxCreateObjectInternal(p, false);
			}
			else
			{
				result = (OdCrypt)Teigha.Core.Helpers.odrxCreateObjectInternal(p, true);
			}
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public new static OdRxClass desc()
		{
			OdRxClass result = (OdRxClass)Teigha.Core.Helpers.odrxCreateObjectInternal(TD_DbPINVOKE.OdCrypt_desc(), false);
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public new virtual OdRxClass isA()
		{
			OdRxClass result = (OdRxClass)Teigha.Core.Helpers.odrxCreateObjectInternal(this.SwigDerivedClassHasMethod("isA", OdCrypt.swigMethodTypes1) ? TD_DbPINVOKE.OdCrypt_isASwigExplicitOdCrypt(this.swigCPtr) : TD_DbPINVOKE.OdCrypt_isA(this.swigCPtr), false);
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public new virtual OdRxObject queryX(OdRxClass protocolClass)
		{
			OdRxObject result = Teigha.Core.Helpers.odrxCreateObjectInternal(this.SwigDerivedClassHasMethod("queryX", OdCrypt.swigMethodTypes0) ? TD_DbPINVOKE.OdCrypt_queryXSwigExplicitOdCrypt(this.swigCPtr, OdRxClass.getCPtr(protocolClass)) : TD_DbPINVOKE.OdCrypt_queryX(this.swigCPtr, OdRxClass.getCPtr(protocolClass)), false);
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static OdCrypt createObject()
		{
			MemoryManager memoryManager = MemoryManager.GetMemoryManager();
			MemoryTransaction currentTransaction = memoryManager.GetCurrentTransaction();
			IntPtr p = TD_DbPINVOKE.OdCrypt_createObject();
			OdCrypt result;
			if (currentTransaction != null)
			{
				currentTransaction.AddObject((OdCrypt)Teigha.Core.Helpers.odrxCreateObjectInternal(p, true));
				result = (OdCrypt)Teigha.Core.Helpers.odrxCreateObjectInternal(p, false);
			}
			else
			{
				result = (OdCrypt)Teigha.Core.Helpers.odrxCreateObjectInternal(p, true);
			}
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public virtual bool initialize(OdSecurityParams securityParams)
		{
			bool result = TD_DbPINVOKE.OdCrypt_initialize(this.swigCPtr, OdSecurityParams.getCPtr(securityParams));
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public virtual bool encryptData(byte[] buffer)
		{
			IntPtr intPtr = Teigha.Core.Helpers.MarshalbyteFixedArray(buffer);
			bool result;
			try
			{
				bool flag = TD_DbPINVOKE.OdCrypt_encryptData(this.swigCPtr, intPtr);
				if (TD_DbPINVOKE.SWIGPendingException.Pending)
				{
					throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
				}
				result = flag;
			}
			finally
			{
				Marshal.FreeCoTaskMem(intPtr);
			}
			return result;
		}

		public virtual bool decryptData(byte[] buffer)
		{
			IntPtr intPtr = Teigha.Core.Helpers.MarshalbyteFixedArray(buffer);
			bool result;
			try
			{
				bool flag = TD_DbPINVOKE.OdCrypt_decryptData(this.swigCPtr, intPtr);
				if (TD_DbPINVOKE.SWIGPendingException.Pending)
				{
					throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
				}
				result = flag;
			}
			finally
			{
				Marshal.FreeCoTaskMem(intPtr);
			}
			return result;
		}

		private void SwigDirectorConnect()
		{
			if (this.SwigDerivedClassHasMethod("queryX", OdCrypt.swigMethodTypes0))
			{
				this.swigDelegate0 = new OdCrypt.SwigDelegateOdCrypt_0(this.SwigDirectorqueryX);
			}
			if (this.SwigDerivedClassHasMethod("isA", OdCrypt.swigMethodTypes1))
			{
				this.swigDelegate1 = new OdCrypt.SwigDelegateOdCrypt_1(this.SwigDirectorisA);
			}
			if (this.SwigDerivedClassHasMethod("initialize", OdCrypt.swigMethodTypes2))
			{
				this.swigDelegate2 = new OdCrypt.SwigDelegateOdCrypt_2(this.SwigDirectorinitialize);
			}
			if (this.SwigDerivedClassHasMethod("encryptData", OdCrypt.swigMethodTypes3))
			{
				this.swigDelegate3 = new OdCrypt.SwigDelegateOdCrypt_3(this.SwigDirectorencryptData);
			}
			if (this.SwigDerivedClassHasMethod("decryptData", OdCrypt.swigMethodTypes4))
			{
				this.swigDelegate4 = new OdCrypt.SwigDelegateOdCrypt_4(this.SwigDirectordecryptData);
			}
			TD_DbPINVOKE.OdCrypt_director_connect(this.swigCPtr, this.swigDelegate0, this.swigDelegate1, this.swigDelegate2, this.swigDelegate3, this.swigDelegate4);
		}

		private bool SwigDerivedClassHasMethod(string methodName, Type[] methodTypes)
		{
			MethodInfo method = base.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, methodTypes, null);
			return method.DeclaringType.IsSubclassOf(typeof(OdCrypt));
		}

		private IntPtr SwigDirectorqueryX(IntPtr protocolClass)
		{
			return OdRxObject.getCPtr(this.queryX((OdRxClass)Teigha.Core.Helpers.odrxCreateObjectInternal(protocolClass, false))).Handle;
		}

		private IntPtr SwigDirectorisA()
		{
			return OdRxClass.getCPtr(this.isA()).Handle;
		}

		private bool SwigDirectorinitialize(IntPtr securityParams)
		{
			return this.initialize(new OdSecurityParams(securityParams, false));
		}

		private bool SwigDirectorencryptData(IntPtr buffer)
		{
			return this.encryptData(Teigha.Core.Helpers.UnMarshalbyteFixedArray(buffer));
		}

		private bool SwigDirectordecryptData(IntPtr buffer)
		{
			return this.decryptData(Teigha.Core.Helpers.UnMarshalbyteFixedArray(buffer));
		}

		private object locker = new object();

		private HandleRef swigCPtr;

		private OdCrypt.SwigDelegateOdCrypt_0 swigDelegate0;

		private OdCrypt.SwigDelegateOdCrypt_1 swigDelegate1;

		private OdCrypt.SwigDelegateOdCrypt_2 swigDelegate2;

		private OdCrypt.SwigDelegateOdCrypt_3 swigDelegate3;

		private OdCrypt.SwigDelegateOdCrypt_4 swigDelegate4;

		private static Type[] swigMethodTypes0 = new Type[]
		{
			typeof(OdRxClass)
		};

		private static Type[] swigMethodTypes1 = new Type[0];

		private static Type[] swigMethodTypes2 = new Type[]
		{
			typeof(OdSecurityParams)
		};

		private static Type[] swigMethodTypes3 = new Type[]
		{
			typeof(byte[])
		};

		private static Type[] swigMethodTypes4 = new Type[]
		{
			typeof(byte[])
		};

		public delegate IntPtr SwigDelegateOdCrypt_0(IntPtr protocolClass);

		public delegate IntPtr SwigDelegateOdCrypt_1();

		public delegate bool SwigDelegateOdCrypt_2(IntPtr securityParams);

		public delegate bool SwigDelegateOdCrypt_3(IntPtr buffer);

		public delegate bool SwigDelegateOdCrypt_4(IntPtr buffer);
	}
}
